package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"mogutouERP/models"
	"mogutouERP/utils/response"
)

// CreateMakeMsg 创建新的制作
func CreateMakeMsg(c *gin.Context) {
	var data struct {
		Goods []struct {
			ID         string `json:"id" binding:"required"`
			Quantity   uint   `json:"quantity" binding:"required"`
			MakePerson string `json:"makePerson" binding:"required"`
		}
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}
	if len(data.Goods) == 0 {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: "没有商品"})
		return
	}

	goods := make([]models.MakeGoods, len(data.Goods))
	for i, v := range data.Goods {
		goods[i].GoodsID = v.ID
		goods[i].Quantity = v.Quantity
		goods[i].MakePerson = v.MakePerson
	}
	err = models.CreateMakeMsg(goods)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, goods)
}

// GetMakeInfo 列举所有制作
func GetMakeInfo(c *gin.Context) {
	goods, err := models.GetMakeInfo()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, goods)
}
